// Head datapump
// Copyright 2006 (C) Ralph Thomas

#include <datapumps/head.h>
#include <binder.hpp>
#include <util/concepts/list.h>
#include <ui/array_datapump_adapter.h>

namespace datapumps {
	struct list_head_t : public util::concept::ListInterface {
		util::concept::List	source;
		unsigned int		n;

		list_head_t(const adobe::any_regular_t& v, unsigned int n) : n(n) {
			source = util::concept::get_list(v);
		}
		virtual ~list_head_t() {}
		virtual std::size_t size() const {
			return std::min(n, source.size());
		}
		virtual adobe::any_regular_t at(unsigned int i) const {
			return source.at(i);
		}
		virtual bool empty() const {
			return source.empty();
		}
	};
	util::concept::ListInstance head(const adobe::any_regular_t& v, unsigned int length) {
		return util::concept::ListInstance(new list_head_t(v, length));
	}
	adobe::any_regular_t make_head_datapump(const adobe::array_t& a, adobe::sheet_t& s) {
		return adobe::any_regular_t( app::array_datapump_adapter_t<util::concept::ListInstance (const adobe::any_regular_t&, unsigned int)>::create( &head, s, a ) );
	}
}
